import { queryDictionaries,  queryUserListForSelect, queryDistrictListForSelect } from '../services/global';
import { queryCurrent } from '../services/user';
import { queryFuzzyInstallationSite} from "../services/equipment";
import { queryMaintainListForSelect } from "../services/maintain";
import { getConfig } from '../../config';
const config = getConfig();
// const baseUrl = process.env.NODE_ENV === "development" ? config.ajaxPrefix : '';
const baseUrl='';

export default {
  namespace: 'global',

  state: {
    baseUrl:baseUrl,
    user_role:[],
    currentUser: {},
    dictionaries: {},
    selectSource:{},
    collapsed: false,
  },

  effects: {

    *fetchCurrent(_, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryCurrent);
      response &&  (
        yield put({
          type: 'appendCurrent',
          payload: response,
        })
      )
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *fetch_change_Current({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield put({
        type: 'changeCurrent',
        payload: payload,
      });
      // yield put({type: 'fetchCurrent'})
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },



    *fetch_current_role(_, { call, put }) {
      const response = yield call(queryCurrent);
      response &&  (
        yield put({
          type: 'appendData',
          payload: { user_role:response.roleAuthority},
        })
      )
    },

    // 获取数据字典
    *fetch_dictionar({ payload }, { call, put, select }) {
      const dictionaries = yield select(state => state.global.dictionaries);
      if(JSON.stringify(dictionaries) == "{}"){
        const response = yield call(queryDictionaries,payload);
        response &&  (
          yield put({
            type: 'appendDictionaries',
            payload:response.data,
          })
        )
      }
    },


    // 获取甲方单位用户列表-----------
    *fetch_select_selfusers({ payload }, { call, put, select  }) {
      const adminUser = yield select(state => state.global.selectSource.adminUser);
      if(!adminUser){
        const response = yield call(queryUserListForSelect, {userType:"01"});
        response && (
          yield put({
            type: 'appendSelectSource',
            payload:{adminUser:response.data.list} ,
          })
        );
      }


    },

    // 获取维保单位下拉列表--------------------------------------
    *fetch_select_maintencompany({ payload }, { call, put, select  }) {
      // const maintenance = yield select(state => state.global.selectSource.maintenance);
      // if(!maintenance){
        const response = yield call(queryMaintainListForSelect, payload);
        response && (
          yield put({
            type: 'appendSelectSource',
            payload:{maintenance:response.data.list} ,
          })
        );
      // }
    },

    // 获取设备养护人、维修人、整改人列表
    *fetch_select_maintenman({ payload }, { call, put }) {
      const response = yield call(queryUserListForSelect, {...payload,userType:"02"});
      response && (
        yield put({
          type: 'appendSelectSource',
          payload:{maintenman:response.data.list} ,
        })
      );

    },

    // 清空人员选择信息
    *fetch_clear_maintenman({ payload }, { call, put }) {
      yield put({
        type: 'appendSelectSource',
        payload:{maintenman:[]} ,
      });
    },



    // 获取区域列表---------------------------
    *fetch_select_district({ payload }, { call, put, select  }) {
      // const districtData = yield select(state => state.global.selectSource.districtData);
      // if(!districtData) {
        const response = yield call(queryDistrictListForSelect, payload);
        response && (
          yield put({
            type: 'appendSelectSource',
            payload:{districtData:response.data.list} ,
          })
        );
      // }
    },

    // 根据区域id查点位
    *fetch_select_point({ payload }, { call, put }) {
      const response = yield call(queryFuzzyInstallationSite, payload)
      response && (
        yield put({
          type: 'appendSelectSource',
          payload:{pointData:response.data.list} ,
        })
      );

    },
    // 清空点位信息
    *fetch_clear_point({ payload }, { call, put }) {
      yield put({
        type: 'appendSelectSource',
        payload:{pointData:[]} ,
      });
    },


  },

  reducers: {
    appendDictionaries(state, action) {
      return {
        ...state,
        dictionaries: action.payload,
      };
    },
    appendSelectSource(state, action) {
      return {
        ...state,
        selectSource: {
          ...state.selectSource,
          ...action.payload,
        },
      };
    },

    appendData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    appendCurrent(state, action) {
      return {
        ...state,
        currentUser: action.payload,
      };
    },
    changeCurrent(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          ...action.payload,
        },
      };
    },

    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },


  },

};
